package com.winning.common.beans.xmlTemp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.winning.common.tools.XMLTool;


/**
 * 类名称：XMLTempBeanManager
 * 类描述：XML配置模板管理器
 * 创建人：liuwensheng
 * 创建时间：2016-12-15 上午8:16:35
 * 修改人：liuwensheng
 * 修改时间：2016-12-15 上午8:16:35
 * 修改备注：
 */
public class XMLTempBeanManager {
    //存放所有配置模板列表
    private Map<String, XMLTempConfigBean> XMLTempConfigBeanMap = null;

    private static XMLTempBeanManager instance = null;

    private static String TEMP_CONFIG_PATH = "com/framework/xmlTempConfig/xmlTempConfig.xml";

    public XMLTempBeanManager() {
        //读取xml模板信息
        XMLTempConfigBeanMap = readXMLTempConfigHandler();
    }

    public static XMLTempBeanManager getInstance() {
        if (instance == null) {
            instance = new XMLTempBeanManager();
        }

        return instance;
    }

    /**
     * 读取模板配置文件地址信息 <br/>
     * 方法名称: readXMLTempConfigHandler
     *
     * @param
     * @return Map<String                                                                                                                               ,                                                                                                                                                                                                                                                               XMLTempConfigBean>  返回类型说明<br/>
     * @throws <br/>
     * @author liuwensheng@winning.com.cn<br/>
     * 创建时间：2016-12-13/下午3:11:02<br/>
     */
    public Map<String, XMLTempConfigBean> readXMLTempConfigHandler() {
        Map<String, XMLTempConfigBean> XMLTempConfigBeanMap = new HashMap<String, XMLTempConfigBean>();

        //加载业务配置文件的路径文件
        Document document = null;
        try {
            //模板管理器配置文件的地址是固定的
            document = XMLTool.getXMLDocument(TEMP_CONFIG_PATH);

            if (document != null) {
                Element rootElement = document.getRootElement();

                for (@SuppressWarnings("unchecked")
                     Iterator<Element> elementIterator = rootElement.elementIterator(); elementIterator.hasNext(); ) {
                    XMLTempConfigBean XMLTempConfigBean = new XMLTempConfigBean();

                    Element childElement = elementIterator.next();

                    Attribute idAttribute = childElement.attribute("serviceID");
                    if (idAttribute != null) {
                        String idValue = idAttribute.getStringValue();
                        XMLTempConfigBean.setServiceID(idValue);
                    }

                    Attribute serviceNameAttribute = childElement.attribute("serviceName");
                    if (serviceNameAttribute != null) {
                        String serviceNameValue = serviceNameAttribute.getStringValue();
                        XMLTempConfigBean.setServiceName(serviceNameValue);
                    }

                    //多数据源配置
                    Attribute dbDataSourceIDAttribute = childElement.attribute("dbDataSourceID");
                    if (dbDataSourceIDAttribute != null) {
                        String dbDataSourceIDValue = dbDataSourceIDAttribute.getStringValue();

                        if (dbDataSourceIDValue != null && !"".equals(dbDataSourceIDValue)) {
                            XMLTempConfigBean.setDbDataSourseID(dbDataSourceIDValue);
                        } else {
                            XMLTempConfigBean.setDbDataSourseID("Default_SQL_Dev");
                        }
                    } else {
                        XMLTempConfigBean.setDbDataSourseID("Default_SQL_Dev");
                    }

                    //模板文件的有效性
                    Attribute jobEnableAttribute = childElement.attribute("jobEnable");
                    if (jobEnableAttribute != null) {
                        boolean jobEnable = "true".equals(jobEnableAttribute.getStringValue()) == true ? true : false;
                        XMLTempConfigBean.setJobEnable(jobEnable);
                    }

                    //作业定时时间表达式
                    Attribute jobExpressionValueAttribute = childElement.attribute("jobExpressionValue");
                    if (jobExpressionValueAttribute != null) {
                        String jobExpressionValue = jobExpressionValueAttribute.getStringValue();
                        XMLTempConfigBean.setJobExpressionValue(jobExpressionValue);
                    }

                    //模板文件的路径
                    Attribute tempFilePathAttribute = childElement.attribute("tempFilePath");
                    if (tempFilePathAttribute != null) {
                        String tempFilePathValue = tempFilePathAttribute.getStringValue();
                        XMLTempConfigBean.setXmlTempFilePath(tempFilePathValue);
                    }

                    //保存模板文件列表信息
                    XMLTempConfigBeanMap.put(XMLTempConfigBean.getServiceID(), XMLTempConfigBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return XMLTempConfigBeanMap;
    }

    public Map<String, XMLTempConfigBean> getXMLTempConfigBeanMap() {
        return XMLTempConfigBeanMap;
    }

    /**
     * 可直接根据配置ID获取模板配置信息 <br/>
     * 方法名称: findXMLTempConfigBeanByID
     *
     * @param
     * @return XMLTempConfigBean  返回类型说明<br/>
     * @throws <br/>
     * @author liuwensheng@winning.com.cn<br/>
     * 创建时间：2016-12-13/下午3:27:07<br/>
     */
    public XMLTempConfigBean findXMLTempConfigBeanByID(String tempFileID) {
        return XMLTempConfigBeanMap.get(tempFileID);
    }


}
